/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009 Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 */
package br.edu.ufcg.ccc.javalog.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import br.edu.ufcg.ccc.javalog.comments.PossibleImprovement;
import br.edu.ufcg.ccc.javalog.controller.FranchiseController;
import br.edu.ufcg.ccc.javalog.exceptions.FranchiseControllerException;
import br.edu.ufcg.ccc.javalog.model.Data;
import br.edu.ufcg.ccc.javalog.model.Franchise;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * View for creating new packages.
 * 
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 08/11/09
 */
@SuppressWarnings("serial")
public class AddPackageView extends AbstractView {
	private Franchise sourceFranchise;
	private Franchise destinationFranchise;

	private AbstractAction actionCancel;
	private JButton buttonCancel;
	private JButton buttonOk;
	private JTextField textNumber;
	private JFormattedTextField textZipcode;
	private JTextField textDistrict;
	private JTextField textStreet;
	private JComboBox comboPackageType;
	private JLabel labelZipcode;
	private JButton buttonDestinationFranchise;
	private JButton buttonSourceFranchise;
	private JLabel labelDestinationFranchise;
	private JLabel labelSourceFranchise;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JFormattedTextField textZipcode2;
	private JTextField textDistrict2;
	private JTextField textStreet2;
	private JLabel jLabel1;
	private JTextField textNumber2;
	private JLabel labelSourceAddress;
	private AbstractAction actionOk;
	private JLabel labelDistrict;
	private JLabel labelNumber;
	private JLabel labelStreet;
	private JLabel labelRegion;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AddPackageView inst = new AddPackageView();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	/**
	 * Creates this view for the specified franchise model object.
	 * @param franchise the model to view
	 */
	public AddPackageView() {
		super();
		initGUI();
	}
		
//	public AddPackageView() {
//		this();
//		
//		//this.franchise = franchise;
//		//this.franchiseController = new FranchiseController(franchise);
//		
//	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
	.addContainerGap()
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getComboRegion(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelRegion(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addGap(20)
	.addComponent(getLabelSourceAddress(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	.addGap(12)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getTextNumber(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelNumber(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getTextStreet(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelStreet(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getTextDistrict(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelDistrict(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getTextZipcode(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelZipcode(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addGap(35)
	.addComponent(getLabelDestination(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getTextNumber2(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getJLabel1(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getTextStreet2(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelStreet2(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getTextDistrict2(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelDistrict2(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getTextZipcode2(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelZipcode2(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addGap(23)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getButtonSourceFranchise(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelSourceFranchise(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getButtonDestinationFranchise(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getLabelDestinationFranchise(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
	.addGap(0, 34, Short.MAX_VALUE)
	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	    .addComponent(getButtonCancel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
	    .addComponent(getButtonOk(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
	.addContainerGap());
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(getLabelDestinationFranchise(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelSourceFranchise(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelRegion(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(getButtonDestinationFranchise(), GroupLayout.Alignment.LEADING, 0, 470, Short.MAX_VALUE)
				            .addComponent(getButtonSourceFranchise(), GroupLayout.Alignment.LEADING, 0, 470, Short.MAX_VALUE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getComboRegion(), GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 363, Short.MAX_VALUE)))
				        .addGap(12))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(getLabelSourceAddress(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getLabelZipcode2(), GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addGap(31))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getLabelDistrict2(), GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addGap(31))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getLabelStreet2(), GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addGap(31))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getLabelZipcode(), GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addGap(31))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getLabelDistrict(), GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addGap(31))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getLabelStreet(), GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				                .addGap(31))
				            .addComponent(getLabelDestination(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE))
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getTextZipcode2(), GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 208, Short.MAX_VALUE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getTextDistrict2(), GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 162, Short.MAX_VALUE))
				            .addComponent(getTextStreet2(), GroupLayout.Alignment.LEADING, 0, 315, Short.MAX_VALUE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getTextZipcode(), GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 208, Short.MAX_VALUE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getTextDistrict(), GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 162, Short.MAX_VALUE))
				            .addComponent(getTextStreet(), GroupLayout.Alignment.LEADING, 0, 315, Short.MAX_VALUE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(getJLabel1(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelNumber(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getTextNumber2(), GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
				                .addGap(10))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getTextNumber(), GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
				                .addGap(10))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addPreferredGap(getTextNumber2(), getButtonOk(), LayoutStyle.ComponentPlacement.INDENT)
				                .addComponent(getButtonOk(), GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(getButtonCancel(), GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap());
			this.setTitle("Criar Novo Pacote");
			pack();
			this.setSize(640, 480);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method should return an instance of this class which does NOT
	 * initialize it's GUI elements. This method is ONLY required by Jigloo if
	 * the superclass of this class is abstract or non-public. It is not needed
	 * in any other situation.
	 */
	public static Object getGUIBuilderInstance() {
		return new AddPackageView(Boolean.FALSE);
	}

	/**
	 * This constructor is used by the getGUIBuilderInstance method to provide
	 * an instance of this class which has not had it's GUI elements initialized
	 * (ie, initGUI is not called in this constructor).
	 */
	private AddPackageView(Boolean initGUI) {
		super();
	}

	private JLabel getLabelRegion() {
		if (labelRegion == null) {
			labelRegion = new JLabel();
			labelRegion.setText("Tipo da encomenda:");
		}
		return labelRegion;
	}

	private JLabel getLabelStreet() {
		if (labelStreet == null) {
			labelStreet = new JLabel();
			labelStreet.setText("Endereço:");
		}
		return labelStreet;
	}

	private JLabel getLabelNumber() {
		if (labelNumber == null) {
			labelNumber = new JLabel();
			labelNumber.setText("Nº:");
		}
		return labelNumber;
	}

	private JLabel getLabelZipcode() {
		if (labelZipcode == null) {
			labelZipcode = new JLabel();
			labelZipcode.setText("CEP:");
		}
		return labelZipcode;
	}

	private JLabel getLabelDistrict() {
		if (labelDistrict == null) {
			labelDistrict = new JLabel();
			labelDistrict.setText("Bairro:");
		}
		return labelDistrict;
	}

	private JComboBox getComboRegion() {
		if (comboPackageType == null) {
			ComboBoxModel comboRegionModel = new DefaultComboBoxModel(
					new String[] { "Normal", "Expressa", "24h" });
			comboPackageType = new JComboBox();
			comboPackageType.setModel(comboRegionModel);
		}
		return comboPackageType;
	}

	private JTextField getTextStreet() {
		if (textStreet == null) {
			textStreet = new JTextField();
		}
		return textStreet;
	}

	private JTextField getTextDistrict() {
		if (textDistrict == null) {
			textDistrict = new JTextField();
		}
		return textDistrict;
	}

	private JFormattedTextField getTextZipcode() {
		if (textZipcode == null) {
			textZipcode = new JFormattedTextField(ViewUtil
					.getZipcodeFormatter());
		}
		return textZipcode;
	}

	private JTextField getTextNumber() {
		if (textNumber == null) {
			textNumber = new JTextField();
		}
		return textNumber;
	}

	private JButton getButtonOk() {
		if (buttonOk == null) {
			buttonOk = new JButton();
			buttonOk.setText("OK");
			buttonOk.setMnemonic(java.awt.event.KeyEvent.VK_O);
			buttonOk.setAction(getActionOk());
		}
		return buttonOk;
	}

	private JButton getButtonCancel() {
		if (buttonCancel == null) {
			buttonCancel = new JButton();
			buttonCancel.setText("Cancelar");
			buttonCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
			buttonCancel.setAction(getActionCancel());
		}
		return buttonCancel;
	}

	private AbstractAction getActionCancel() {
		if (actionCancel == null) {
			actionCancel = new AbstractAction("Cancelar", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
			actionCancel.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(
					java.awt.event.KeyEvent.VK_C));
		}
		return actionCancel;
	}

	@PossibleImprovement(author = "Victor Freire", date = "01/12/09", description = "Log unexpected errors.")
	private AbstractAction getActionOk() {
		if (actionOk == null) {
			actionOk = new AbstractAction("OK", null) {
				public void actionPerformed(ActionEvent evt) {
					String street = textStreet.getText(),
						   number = textNumber.getText(),
						   zipcode = textZipcode.getText(),
						   district = textDistrict.getText(),
						   street2 = textStreet2.getText(),
						   number2 = textNumber2.getText(),
						   zipcode2 = textZipcode2.getText(),
						   district2 = textDistrict2.getText();
					
					String packageType = (String)comboPackageType.getSelectedItem();
							
					if(sourceFranchise == null) {
						JOptionPane.showMessageDialog(AddPackageView.this, "Não foi escolhida a Franquia de Origem.", "Erro", JOptionPane.ERROR_MESSAGE);
						return;
					}
					else if(destinationFranchise == null) {
						JOptionPane.showMessageDialog(AddPackageView.this, "Não foi escolhida a Franquia de Destino.", "Erro", JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					FranchiseController franchiseController = new FranchiseController(sourceFranchise);
					
					try {
						franchiseController.createPackage(packageType, destinationFranchise, street, number, zipcode, district, street2, number2, zipcode2, district2);
						JOptionPane.showMessageDialog(AddPackageView.this, "Nova pacote criado com sucesso!");
						dispose();
					} catch (FranchiseControllerException e) {
						JOptionPane.showMessageDialog(AddPackageView.this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
					}
				}
			};
			actionOk.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(
					java.awt.event.KeyEvent.VK_O));
		}
		return actionOk;
	}

	private JLabel getLabelSourceAddress() {
		if (labelSourceAddress == null) {
			labelSourceAddress = new JLabel();
			labelSourceAddress.setText("Remetente");
			labelSourceAddress.setFont(new java.awt.Font("Tahoma", 1, 12));
		}
		return labelSourceAddress;
	}

	private JTextField getTextNumber2() {
		if (textNumber2 == null) {
			textNumber2 = new JTextField();
		}
		return textNumber2;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Nº:");
		}
		return jLabel1;
	}

	private JTextField getTextStreet2() {
		if (textStreet2 == null) {
			textStreet2 = new JTextField();
		}
		return textStreet2;
	}

	private JTextField getTextDistrict2() {
		if (textDistrict2 == null) {
			textDistrict2 = new JTextField();
		}
		return textDistrict2;
	}

	private JFormattedTextField getTextZipcode2() {
		if (textZipcode2 == null) {
			textZipcode2 = new JFormattedTextField(ViewUtil
					.getZipcodeFormatter());
		}
		return textZipcode2;
	}

	private JLabel getLabelDestination() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Destinatário");
			jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
		}
		return jLabel2;
	}

	private JLabel getLabelStreet2() {
		if (jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("Endereço:");
		}
		return jLabel3;
	}

	private JLabel getLabelDistrict2() {
		if (jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("Bairro:");
		}
		return jLabel4;
	}

	private JLabel getLabelZipcode2() {
		if (jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("CEP:");
		}
		return jLabel5;
	}
	
	private JLabel getLabelSourceFranchise() {
		if(labelSourceFranchise == null) {
			labelSourceFranchise = new JLabel();
			labelSourceFranchise.setText("Franquia de Origem:");
		}
		return labelSourceFranchise;
	}
	
	private JLabel getLabelDestinationFranchise() {
		if(labelDestinationFranchise == null) {
			labelDestinationFranchise = new JLabel();
			labelDestinationFranchise.setText("Franquia de Destino:");
		}
		return labelDestinationFranchise;
	}
	
	private JButton getButtonSourceFranchise() {
		if(buttonSourceFranchise == null) {
			buttonSourceFranchise = new JButton();
			buttonSourceFranchise.setText("Clique aqui para definir a origem");
			buttonSourceFranchise.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					buttonSourceFranchiseActionPerformed(evt);
				}
			});
		}
		return buttonSourceFranchise;
	}
	
	private JButton getButtonDestinationFranchise() {
		if(buttonDestinationFranchise == null) {
			buttonDestinationFranchise = new JButton();
			buttonDestinationFranchise.setText("Clique aqui para definir o destino");
			buttonDestinationFranchise.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					buttonDestinationFranchiseActionPerformed(evt);
				}
			});
		}
		return buttonDestinationFranchise;
	}	
	
	private void buttonSourceFranchiseActionPerformed(ActionEvent evt) {
		Franchise franchise = SearchFranchiseDialog.showDialog(AddPackageView.this, AddPackageView.this, Data.getInstance().getHeadquarters());
		if(franchise != null) {
			if(destinationFranchise != null && destinationFranchise.equals(franchise)) {
				JOptionPane.showMessageDialog(AddPackageView.this, "A origem e o destino precisam ser diferentes.", "Erro", JOptionPane.ERROR_MESSAGE);
			}
			else {
				sourceFranchise = franchise;
				buttonSourceFranchise.setText(sourceFranchise.toString());
			}
		}
	}
	
	private void buttonDestinationFranchiseActionPerformed(ActionEvent evt) {
		Franchise franchise = SearchFranchiseDialog.showDialog(AddPackageView.this, AddPackageView.this, Data.getInstance().getHeadquarters());
		if(franchise != null) {
			if(sourceFranchise != null && sourceFranchise.equals(franchise)) {
				JOptionPane.showMessageDialog(AddPackageView.this, "A origem e o destino precisam ser diferentes.", "Erro", JOptionPane.ERROR_MESSAGE);
			}
			else {
				destinationFranchise = franchise;
				buttonDestinationFranchise.setText(destinationFranchise.toString());
			}
		}
	}

}
